<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
class Clean extends BaseController
{
    public function index(){
        $clean=Db::name("clean")->select();
        $this->assign("clean",$clean);
        $types=Db::name("type")->select();
        $this->assign("types",$types);
        $cleans=Db::query('select * from type t,clean c where t.tid=c.tid order by c.clid desc');
        $this->assign("clean",$cleans);
        return $this->fetch();
    }
    public function add(){
        //上传
        // 获取表单上传文件
        $info="";
        $file = request()->file("pic");//手册里上传文件
        if($file){
            $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'static'.DS.'assest'.DS.'images');
            //取值
            if(request()->isPost()){
                $data=['cleanname'=>input('param.cleanname'),
                    'tid' => input('type'),
                    'uploadtime'=>date('Y-m-d h-m-s'),
                    'uploaduser'=>2,
                    'pic'=>$info->getFilename(),
                    //完成上传后修改
                    'intro' => input('intro'),
                    'hittimes'=>0,];
            }
            //存入数据库
            var_dump($data);
            $res=Db::name("clean")->insert($data);
            if($res){
                $this->success("添加卸妆成功");
            }
            else{
                $this->error("添加卸妆失败");
            }
        }
    }
    public function delete($id){
        if(Db::name("clean")->delete($id))
        {
            $this->success('删除成功');
        }else
            $this->error('删除失败');

    }
    public function update()
    {
        if (request()->isPost())
        {
            $file=request()->file("pic");
            if($file!=null)
            {
                $info=$file->move('static/assest/images/',date("YmdHis").rand(100,999));
                if($info)
                {
                    $ddr=Db::name("clean")->find(input("clid"));
                    unlink("static/assest/images/".$ddr["pic"]);
//                    echo $_POST["type"];
                    $data = [
                        'clid'=>input("clid"),
                        'cleanname' => input('cleanname'),
                        'tid' => input('type'),//$_POST["type"],
                        'pic' => $info->getSaveName(),
                        'intro' => input('intro'),
                        'uploadtime' => input('uploadtime'),
                    ];//JSON Data
                    $res = Db::table('clean')->update($data);
                    if ($res)
                    {
                        $this->success("修改卸妆成功");
                    }
                    else
                    {
                        $this->error("修改卸妆失败");
                    }
                }
                else
                {
                    $this->error("上传失败");
                }
            }
            else
            {
                $data = [
                    'clid'=>input("clid"),
                    'cleanname' => input('cleanname'),
                    'tid' => $_POST["type"],
                    'intro' => input('intro'),
                    'uploadtime' => input('uploadtime'),
                ];//JSON Data
                $res = Db::table('clean')->update($data);
                if ($res)
                {
                    $this->success("修改卸妆成功");
                }
                else
                {
                    $this->error("修改卸妆失败");
                }
            }

        }
    }

}